<?php

/**
 * @copyright: ©2024 永州微聚商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */

namespace app\mobile\controller;



use app\admin\model\ConsumerClaimLog;
use app\admin\model\Platform;
use app\admin\model\StoreAccount;
use app\admin\model\TicketAddress;
use app\admin\model\TicketConsumer;
use app\admin\model\TicketOrder;
use app\BaseController;
use think\App;


/**
 * 用户操作
 *
 * @author Administrator
 *
 */
class Ticket extends BaseController
{

    public function addTicket(){
        $request = $this->request;
        $param = $request->param();

        $storeModel  = new \app\admin\model\Store();
        $storeInfo = $storeModel->where(['id' =>$param['store_id'] ])->find();
        if ( $storeInfo['is_application'] != 2){
            return result(-1, '店铺未进件');
        }
        $data = [
            'name'=>$param['name'],
            'img'=>$param['img'],
            'price'=>$param['price'],
           // 'start_time'=> strtotime($param['start_time']) ,
            'end_time'=> strtotime($param['end_time']) ,
            'platform_id'=> $request->platform_id ,
            'desc'=> $param['desc'],
            'store_id' => $param['store_id'],
            'inventory' => $param['inventory'],
            'sales' => $param['sales'],
            'scenic_img' => $param['scenic_img'],
        ];
        $ticketModel = new \app\admin\model\Ticket();
        $ticketModel->save($data);
        foreach ($param['address'] as $value){
            $addressData = [
                'ticket_id' => $ticketModel->id,
                'latitude' => $value['latitude'],
                'longitude' => $value['longitude'],
                'address' => $value['address'],
            ];
            $ticketAddressModel = new TicketAddress();
            $ticketAddressModel->save($addressData);
        }
        return result(0, '添加成功',['id' =>$ticketModel->id]);
    }


    public function editTicket(){
        $request = $this->request;
        $param = $request->param();
        $data = [
            'name'=>$param['name'],
            'img'=>$param['img'],
            'price'=>$param['price'],
         //  'start_time'=> strtotime($param['start_time']) ,
          //  'platform_id'=> $request->platform_id ,
            'end_time'=> strtotime($param['end_time']) ,
            'desc'=> $param['desc'],
            'store_id' => $param['store_id'],
        ];
        $ticketModel = new \app\admin\model\Ticket();
        $ticketAddressModel = new TicketAddress();
        $ticketModel->where(['id' => $param['id'] ])->update($data);
        $ticketAddressModel->where(['ticket_id' => $param['id']])->update(['delete_time' => time()]);

        foreach ($param['address'] as $value){
            $addressData = [
                'ticket_id' => $param['id'],
                'latitude' => $value['latitude'],
                'longitude' => $value['longitude'],
                'address' => $value['address'],
            ];
            $ticketAddressModel = new TicketAddress();
            $ticketAddressModel->save($addressData);
        }
        return result(0, '修改成功',['id' =>$ticketModel->id]);
    }


    public function ticketList(){
        $request = $this->request;
        $param = $request->param();
        $where[] = ['ticket.is_delete','=',0];
        $where[] = ['ticket.platform_id','=', $request->platform_id];
        $where[]= ['platformInfo.expiration_time','>',time()];
        if (isset( $param['name']) &&  $param['name']){
            $where[] = ['ticket.name','like',"%{$param['name']}%"];
        }
        if (isset( $param['status']) &&  $param['status']){
            $where[] = ['ticket.status','=',$param['status']];
            if ($param['status'] == 2  ){
                $where[] = ['ticket.end_time','>',time()];
            }
        }
        if (isset( $param['store_id']) &&  $param['store_id']){
            $where[] = ['store_id','=',$param['store_id']];
        }
        $pindex = max(1, intval(input('page')));
        $psize = empty(input('psize')) ? 10 : intval(input('psize'));
        $ticketModel = new \app\admin\model\Ticket();
        $total = $ticketModel->withJoin(['storeInfo','platformInfo'], 'LEFT')
            ->where($where)->count();

        if ($total) {
            $list = $ticketModel->withJoin(['storeInfo','platformInfo'], 'LEFT')
                ->where($where)->order('create_time', 'desc')
                ->limit(($pindex - 1) * $psize, $psize)
                ->select();
            foreach ($list as  $key => $value){
                $couponConsumerModel = new TicketConsumer();
                $couponConsumerList =  $couponConsumerModel->where(['ticket_id'=> $value['id']])->select()->toArray();
                $couponConsumerCount=  $couponConsumerModel->where(['ticket_id'=> $value['id'],'status' => 1])->count();
                $list[$key]['store_count'] = $couponConsumerCount;
                $consumerId  = array_column($couponConsumerList,'id');
                $ConsumerModel = new \app\admin\model\Consumer();
                $list[$key]['consumer'] = $ConsumerModel->where('id','in',$consumerId)->select();
                $ticketAddressModel = new TicketAddress();
                $ticketAddressList = $ticketAddressModel->where(['ticket_id' =>$value['id'] ])->select();
                $list[$key]['address'] = $ticketAddressList;
                $list[$key]['start_time'] = date('Y-m-d',$value['start_time']);
                $list[$key]['end_time'] = date('Y-m-d',$value['end_time']);
                $list[$key]['total_sales'] = $value['sales'] +$value['real_sales'] ;
            }


        } else {
            $list = array();
        }

        $totalPage = ceil($total / $psize);
        return result(0, 'success', array(
            'list' => $list,
            'total' => $total,
            'pindex' => $pindex,
            'psize' => $psize,
            'totalPage' => $totalPage
        ));
    }

    public function joinTicket(){
        $request = $this->request;
        $param = $request->param();
        $TicketConsumer  =  new TicketConsumer();
        $storeModel  = new \app\admin\model\Store();
        $storeInfo = $storeModel->where(['id' =>$param['store_id'] ])->find();
        if ( $storeInfo['is_application'] != 2){
            return result(-1, '店铺未进件');
        }
        //foreach ($param[''] as )
        $TicketConsumerInfo =  $TicketConsumer->where([

            'store_id' => $param['store_id'],
            'ticket_id' => $param['ticket_id'],

        ])->find();
        if ($TicketConsumerInfo){
            if ($TicketConsumerInfo['status'] == 1 ||$TicketConsumerInfo['status'] == 2 )
            return result(-1, '此店铺已加入此票根');
        }
        $Ticket =  new \app\admin\model\Ticket();
        $TicketInfo = $Ticket->where(['id' =>  $param['ticket_id']])->find();
        if ($TicketInfo['status'] != 1){
            return result(-1, '活动已开始无法加入');
        }

        $data = [
            'consumer_id' => $param['consumer_id'],
            'store_id' => $param['store_id'],
            'ticket_id' => $param['ticket_id'],
            'platform_id' => $request->platform_id,
            'provide_store_id'=>  $TicketInfo['store_id']
        ];
        $TicketConsumer->save($data);
        return result(0, '申请成功等待审核');
    }


    public function ticketStatus(){
        $request = $this->request;
        $param = $request->param();
        $ticketModel = new \app\admin\model\Ticket();
        $ticketModel->where(['id' => $param['id']])->update(['status' => $param['status']]);
        return result(0, '操作成功');
    }



    public function ticketInfo (){
        $request = $this->request;
        $param = $request->param();
        $ticketModel = new \app\admin\model\Ticket();
        $ticketInfo = $ticketModel->withJoin(['storeInfo'
        ], 'LEFT')->where(['ticket.id' =>$param['id']])->find();
        $ticketInfo['scenic_img']  = explode(',', $ticketInfo['scenic_img']);
        $ticketInfo['start_time'] = date('Y-m-d',$ticketInfo['start_time']);
        $ticketInfo['end_time'] = date('Y-m-d',$ticketInfo['end_time']);
        $ticketAddressModel = new TicketAddress();
        $ticketAddressList = $ticketAddressModel->where(['ticket_id' => $param['id']])->select();
        $ticketInfo['ticket_address'] = $ticketAddressList;
        $ticketConsumerModel = new \app\admin\model\TicketConsumer();
        $where = [];
        if (isset( $param['status']) &&  $param['status']){
            $where[] = ['ticket_consumer.status','=',$param['status']];
        }else{
            $where[] = ['ticket_consumer.status','=',2];
        }
        $consumerList  = $ticketConsumerModel
            ->withJoin(['consumerInfo','storeInfo'
            ], 'LEFT')
            ->where($where)
            ->where(['ticket_id' => $param['id']])->select()->toArray();
//        if ($ticketConsumerList){
//            $consumerId = array_column($ticketConsumerList,'consumer_id');
//        }
//        $consumerList = [];
//        if ($consumerId){
//            $consumerList  = $ConsumerModel
//                ->withJoin(['storeInfo'], 'LEFT')
//                ->where('consumer.id','in',$consumerId)->select();
//        }
         $ticketInfo['consumer_list'] = $consumerList;
         $ticketInfo['store_join_status'] = 0;
        $ticketInfo['total_sales'] = $ticketInfo['sales'] + $ticketInfo['real_sales'] ;
        if (isset($param['store_id']) && $param['store_id']){
            $ticketConsumerInfo  = $ticketConsumerModel
                ->where(['ticket_id' => $param['id'],'store_id' =>$param['store_id'] ])->find();
            if ($ticketConsumerInfo){
                $ticketInfo['store_join_status'] = $ticketConsumerInfo['status'];
            }
        }
        $ticketInfo['is_buy'] = 0;
        if (isset($param['uid']) && $param['uid']){
            $ticketOrderModel  = new TicketOrder();
            $ticketOrderInfo  = $ticketOrderModel
                ->where(['ticket_id' => $param['id'],'uid' =>$param['uid'],'pay_status' =>  2 ])->find();
            if ($ticketOrderInfo){
                $ticketInfo['is_buy'] =1;
            }
        }
        return result(0, '操作成功',$ticketInfo);

    }


    public function ticketConsumer(){
        $request = $this->request;
        $param = $request->param();
        $ticketConsumerModel = new TicketConsumer();
        $pindex = max(1, intval(input('page')));
        $psize = empty(input('psize')) ? 10 : intval(input('psize'));
        $where = [];
        if (isset( $param['status']) &&  $param['status']){
            $where[] = ['ticket_consumer.status','=',$param['status']];
        }
        $total = $ticketConsumerModel
            ->withJoin(['consumerInfo','storeInfo'], 'LEFT')
            ->where(['ticket_id'=> $param['id']])->count();
        if ($total) {
            $list = $ticketConsumerModel
                ->withJoin(['consumerInfo','storeInfo'], 'LEFT')
                ->where(['ticket_id'=> $param['id']])->order('create_time', 'desc')
                ->limit(($pindex - 1) * $psize, $psize)
                ->select();;
        } else {
            $list = array();
        }
        $totalPage = ceil($total / $psize);
        return result(0, 'success', array(
            'list' => $list,
            'total' => $total,
            'pindex' => $pindex,
            'psize' => $psize,
            'totalPage' => $totalPage
        ));
    }

    public function buyTicket(){
       try{
           $request = $this->request;
           $param = $request->param();
           $ticketModel = new \app\admin\model\Ticket();
           $ticketInfo = $ticketModel->where(['id' =>$param['id'] ])->find();
           if ($ticketInfo['inventory'] <= 0){
               return result(-1, '库存不足' );
           }
           $data = [
                'uid' => $param['uid'],
                'order_number' => getPayNo('cou'),
                'platform_id' => $request->platform_id,
                'ticket_id' => $param['id'],
                'price' => $ticketInfo['price'],
                'store_id' => $ticketInfo['store_id'],
                'start_time' => $ticketInfo['start_time'],
                'end_time' => $ticketInfo['end_time'],
                'is_use' => 1,
                'way_code' => $param['way_code'],
                'code' => ticketCode()

            ];
           $ticketOrderModel = new TicketOrder();
           $ticketOrderInfo = $ticketOrderModel->where([  'ticket_id' => $param['id'],
                'uid' => $param['uid'],'pay_status'=> 2])->find();
           if ($ticketOrderInfo){
               return result(-1, '已购买' );
           }
           $ticketOrderModel->save($data);
       // $domain = $request->domain().'/h5/pay_index.html?ticket_order_id='.$ticketOrderModel->id.'#/ticket_order_id='.$ticketOrderModel->id;
           $web_share_img  = common_qr_code($ticketOrderModel->id.'&'.'2','store', $request->platform_id);
           $ticketOrderModel->where(['id' => $ticketOrderModel->id])->update(['web_share_img' =>$web_share_img ]);
           $ticketConsumerModel = new \app\admin\model\TicketConsumer();
           $ticketConsumerList = $ticketConsumerModel->where(['ticket_id' =>$param['id'] ])->select();
           if ($ticketInfo['price'] >0){
               $ticketOrderModel->where(['id' => $ticketOrderModel->id])->update(['pay_status' => 1]);
               return result(0, '待支付', ['id' =>$ticketOrderModel['id'] ,'code'=> 1]);
           }else{
               foreach ($ticketConsumerList as $value){
                   $consumerModel = new \app\admin\model\Consumer();
                   $consumerClaimLog = new ConsumerClaimLog();
                   $consumerInfo = $consumerModel->where(['id' =>  $value['consumer_id']])->find();
                   $ticketConsumerData = [
                       'consumer_id' => $value['consumer_id'],
                       'order_number' => $data['order_number'],
                       'store_id'  => $value['store_id'],
                       'platform_id'  => $request->platform_id,
                       'uid' => $param['uid'],
                       'type'=> 2,
                       'status'=> 1,
                       'expire_time' => time()+ ($consumerInfo['expire_time'] *86400 ),
                       'price' =>0,
                       'consumer_type' => $consumerInfo['consumer_type'],
                       'satisfy_amount' => $consumerInfo['satisfy_amount'],
                       'deductible_amount' => $consumerInfo['deductible_amount'],
                       'ticket_id' => $param['id'],
                       'order_id' => $ticketOrderModel->id,
                       'issue_type' => $consumerInfo['issue_type'],
                       'send_way' => 4,
                       'write_store_id' =>  $value['store_id'],
                       'verification_code' =>consumerVerificationCode()
                   ];
                   $consumerClaimLog->save($ticketConsumerData);
                   $settlement_code  = common_qr_code($consumerClaimLog->id.'&'.'1' ,'consumer', $request->platform_id);
                   $consumerClaimLog->where(['id' => $consumerClaimLog->id])->update(['settlement_code' =>$settlement_code ]);
                   $consumerModel->where(['id' => $value['consumer_id']])->inc('claimed_number')->update();
               }
               $ticketOrderModel->where(['id' => $ticketOrderModel->id])->update(['pay_status' => 2]);

               $ticketModel->where(['id' =>$param['id'] ])->inc('real_sales')->dec('inventory')->update();
               return result(0, '领取成功', ['id' =>$ticketOrderModel['id'] ,'code'=> 0]);
           }
       }catch (\Exception $exception){
           return result(-1, '失败' );
       }

    }

    public function  ticketOrder(){
        $request = $this->request;
        $param = $request->param();
        $couponOrderModel = new \app\admin\model\TicketOrder();
      //  $where[]  =['platform_id' ,'=', $request->platform_id];
        $where[]  =['pay_status' ,'=', 2];
        if (isset($param['uid']) &&$param['uid'] ){
            $where[]  = ['uid' ,'=', $param['uid']];
        }
        if (isset($param['store_id']) && $param['store_id'] ){
            $where[]  = ['store_id' ,'=', $param['store_id']];
        }
        if (isset($param['is_use']) &&$param['is_use'] ){

            if ($param['is_use'] == 3){
                $where[]  = ['end_time' ,'<', time()];
            }else{
                $where[]  = ['is_use' ,'=', $param['is_use']];
            }

        }



        $pindex = max(1, intval(input('page')));
        $psize = empty(input('psize')) ? 10 : intval(input('psize'));
        $total = $couponOrderModel
            ->withJoin('ticketInfo', 'LEFT')
            ->where($where)->count();
        if ($total) {
            $list = $couponOrderModel
                ->withJoin('ticketInfo', 'LEFT')
                ->where($where)->order('create_time', 'desc')
                ->limit(($pindex - 1) * $psize, $psize)
                ->select();
        } else {
            $list = array();
        }
        foreach ($list as$key   => $value){
            $consumerClaimLogModel = new \app\admin\model\ConsumerClaimLog();
            $consumerList = $consumerClaimLogModel->withJoin([
                'consumerInfo'
            ], 'LEFT')

                ->where(['order_id' => $value['id'],'send_way' => '4'])
                ->select();
            $list[$key]['consumer_list'] =$consumerList;
            $list[$key]['consumer_count'] = count($consumerList);
            $ticketList = $couponOrderModel->where(['ticket_id' => $value['ticket_id'],'pay_status' =>2 ])->select()->toArray();
            $list[$key]['user_list']  = [];
            if ($ticketList){
                $uid = array_column($ticketList,'uid');
                $userModel  = new \app\admin\model\User();
                $userList = $userModel->where('id','in',$uid)->select();
                $list[$key]['user_list'] =$userList;
            }

//            foreach ($ticketList as $ticketKey => $ticketValue){
//
//            }

        }

        $totalPage = ceil($total / $psize);
        return result(0, 'success', array(
            'list' => $list,
            'total' => $total,
            'pindex' => $pindex,
            'psize' => $psize,
            'totalPage' => $totalPage
        ));
    }

    public function joinTicketList(){
        $request = $this->request;
        $param = $request->param();
        $TicketConsumer  =  new TicketConsumer();
        $where =[];
        if ( isset($param['provide_store_id']) && $param['provide_store_id'] ){
            $where['provide_store_id'] = $param['provide_store_id'];
        }
        if ( isset($param['ticket_id']) && $param['ticket_id'] ){
            $where['ticket_id'] = $param['ticket_id'];
        }
        if ( isset($param['store_id']) && $param['store_id'] ){
            $where['ticket_consumer.store_id'] = $param['store_id'];
        }
        if ( isset($param['status']) && $param['status'] ){
            $where['ticket_consumer.status'] = $param['status'];
        }
        $pindex = max(1, intval(input('page')));
        $psize = empty(input('psize')) ? 10 : intval(input('psize'));
        $total = $TicketConsumer->withJoin(['storeInfo','ticketInfo','consumerInfo'], 'LEFT')
            ->where($where)->count();
        $list=  $TicketConsumer
            ->withJoin(['storeInfo','ticketInfo','consumerInfo'], 'LEFT')
            ->limit(($pindex - 1) * $psize, $psize)
            ->where($where)
            ->order('ticket_consumer.create_time', 'desc')
            ->select();
        foreach ($list as $key => $value){
            $list[$key]['consumerInfo']['expire_time'] = date('Y-m-d',$value['consumerInfo']['expire_time']);
        }
        $totalPage = ceil($total / $psize);
        return result(0, 'success', array(
            'list' => $list,
            'total' => $total,
            'pindex' => $pindex,
            'psize' => $psize,
            'totalPage' => $totalPage
        ));
    }

    // 审核加入店铺
    public function review(){
        $request = $this->request;
        $param = $request->param();

        $TicketConsumer  =  new TicketConsumer();
        $templateModel   = new \app\admin\model\Template();
        $ticketConsumerInfo = $TicketConsumer->where(['id' =>$param['id'] ])->find();

        $TicketConsumer->where(['id' => $param['id']])->update(['status' => $param['status']]);

        $ticketModel = new \app\admin\model\Ticket();
        $ticketInfo = $ticketModel->where(['id' =>$ticketConsumerInfo['ticket_id'] ])->find();

        $platform  = new Platform();
        $platformInfo =  $platform->where(['id' => $ticketInfo['platform_id']])->find();

        $storeModel = new \app\admin\model\Store();
        $storeInfo = $storeModel->where(['id'=> $ticketInfo['store_id']])->find();


        $userModel = new \app\admin\model\User();
        $userInfo  = $userModel->where(['id' => $storeInfo['uid']])->find();

        $templateInfo = $templateModel->where(['name'=>'ticket_review',
            'platform_id'=> $ticketInfo['platform_id']])->find();
        $withdraw = new \app\admin\model\template\TicketReview();

        if ($templateInfo){
            if ($userInfo['openid']){
                $sendData = [
                    'template_id' => $templateInfo['template_id'],
                    'appid' => $platformInfo['xcx_appid'],
                    'appsecret' => $platformInfo['xcx_appsecret'],
                    'openid' => $userInfo['openid'],
                    'desc' => $param['status'] == 2 ? '审核通过':'驳回'  ,
                    'name' => '票根'.$ticketInfo['name'],


                ];
                $withdraw->send($sendData);
            }

        }

        return result(0, '操作成功');

    }

    public function useTicket(){
        $request = $this->request;
        $param = $request->param();
        $TicketOrder =  new TicketOrder();
        $TicketOrderInfo = $TicketOrder->where(['id' =>$param['id'] ]) ->find();
        if($TicketOrderInfo['end_time'] < time()){
            return result(0, '票根过期');
        }
        $ticketModel = new \app\admin\model\Ticket();
        $ticketInfo  =$ticketModel->where(['id' =>$TicketOrderInfo['ticket_id'] ])->find();
        $account = new StoreAccount();
        $accountInfo =  $account->where(['id' => $param['account_id']])->find();
        if ($accountInfo['store_id'] != $TicketOrderInfo['store_id'] ){
            return result(0, '此票根不属于此该店铺');
        }
        $platform  = new Platform();
        $platformInfo =  $platform->where(['id' => $TicketOrderInfo['platform_id']])->find();
        $userModel = new \app\admin\model\User();
        $userInfo  = $userModel->where(['id' => $TicketOrderInfo['uid']])->find();
        $withdraw = new \app\admin\model\template\Ticket();
        $templateModel   = new \app\admin\model\Template();
        $templateInfo = $templateModel->where(['name'=>'ticket',
            'platform_id'=> $TicketOrderInfo['platform_id']])->find();
        if ($userInfo['openid']){
            $sendData = [
                'template_id' => $templateInfo['template_id'],
                'appid' => $platformInfo['xcx_appid'],
                'appsecret' => $platformInfo['xcx_appsecret'],
                'openid' => $userInfo['openid'],
                'price' => $TicketOrderInfo['price'],
                'amount' => $TicketOrderInfo['amount'],
                'name' => '票根--'.$ticketInfo['name'],
            ];
            $withdraw->send($sendData);
        }
        $TicketOrder->where(['id' =>$param['id'] ])->update([
            'is_use' => 2,
            'account_id' => $param['account_id'],
            'operation_uid' => $param['operation_uid'],
            ]);
        return result(0, '核销成功');
    }

    public  function ticketBuyInfo(){
        $request = $this->request;
        $param = $request->param();
        $couponOrderModel = new \app\admin\model\TicketOrder();

        $where[]  =['pay_status' ,'=', 2];
        if (isset($param['uid']) &&$param['uid'] ){
            $where[]  = ['uid' ,'=', $param['uid']];
        }

        if (isset($param['nickname']) &&$param['nickname'] ){
            $where[]  = ['uid' ,'=', $param['uid']];
        }
        if (isset($param['store_id']) && $param['store_id'] ){
            $where[]  = ['store_id' ,'=', $param['store_id']];
        }
        if (isset($param['is_use']) &&$param['is_use'] ){

            if ($param['is_use'] == 3){
                $where[]  = ['end_time' ,'<', time()];
            }else{
                $where[]  = ['is_use' ,'=', $param['is_use']];
            }

        }

        if (isset($param['is_use']) &&$param['is_use'] ){

            if ($param['is_use'] == 3){
                $where[]  = ['end_time' ,'<', time()];
            }else{
                $where[]  = ['is_use' ,'=', $param['is_use']];
            }

        }
        if (isset($param['ticket_id']) &&$param['ticket_id'] ){
            $where[]  = ['ticket_id' ,'=', $param['ticket_id']];
        }
        $pindex = max(1, intval(input('page')));
        $psize = empty(input('psize')) ? 10 : intval(input('psize'));
        $total = $couponOrderModel->where($where)->count();
        if ($total) {
            $list = $couponOrderModel
                ->withJoin('userInfo', 'LEFT')
                ->where($where)->order('create_time', 'desc')
                ->limit(($pindex - 1) * $psize, $psize)
                ->select();
        } else {
            $list = array();
        }

        $data['pay_amount'] = $couponOrderModel->where(['pay_status' => 2])->sum('price');
        $data['wait_use'] = $couponOrderModel->where(['is_use' => 1])->count();
        $data['already_use'] = $couponOrderModel->where(['is_use' => 2])->count();
        $data['expired'] = $couponOrderModel->where(['is_use' => 3])->count();


        $totalPage = ceil($total / $psize);
        return result(0, 'success', array(
            'list' => $list,
            'total' => $total,
            'pindex' => $pindex,
            'psize' => $psize,
            'totalPage' => $totalPage,
            'data' => $data
        ));
    }


    public function orderInfo(){
        $request = $this->request;
        $param = $request->param();
        $couponOrderModel = new \app\admin\model\TicketOrder();
        if (isset($param['id']) &&$param['id'] ){
            $where[]  = ['ticket_order.id' ,'=', $param['id']];
        }


        if (isset($param['code']) &&$param['code'] ){
            $where[]  = ['ticket_order.code' ,'=', $param['code']];
        }
        $list = $couponOrderModel            ->withJoin(['userInfo','storeInfo','ticketInfo'], 'LEFT')

            ->where($where)
            ->find();

        if (empty($list)){
            return result(-1, '数据不存在');
        }
        $ticketAddressModel = new TicketAddress();

        if ($list['is_use'] == 1  && $list['end_time'] < time()){
            $list['is_use'] = 3;
        }
        $list['end_time'] = date('Y-m-d',$list['end_time']);
        $ticketAddressList=  $ticketAddressModel->where(['ticket_id' => $list['ticket_id']])->select();
        $list['ticket_address'] = $ticketAddressList;
        return result(0, '操作成功',$list);

    }



}